using System;
using System.Data;
using System.Data.OleDb;
using PCSComUtils.Common;
using PCSComUtils.DataAccess;
using PCSComUtils.PCSExc;

namespace PCSComProduction.WorkOrder.DS
{
	/// <summary>
	/// Summary description for PRO_IssueSubMaterialDetailDS.
	/// </summary>
	public class PRO_IssueSubMaterialDetailDS 
	{
		private const string THIS = "PCSComProduction.WorkOrder.DS.PRO_IssueSubMaterialDetailDS";
		public PRO_IssueSubMaterialDetailDS()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		
		public Object GetObjectVO(int pintID)
		{
			throw new NotImplementedException();
		}

		public DataSet List()
		{
			throw new NotImplementedException();
		}

		public void Delete(int pintID)
		{
			const string METHOD_NAME = THIS + ".Delete()";
			string strSql = String.Empty;
			strSql=	"DELETE " + Pro_IssueSubMaterialDetailTable.TABLE_NAME + " WHERE  " + Pro_IssueSubMaterialDetailTable.ISSUESUBMATERIALMASTERID_FLD + "=" + pintID.ToString();
			OleDbConnection oconPCS=null;
			OleDbCommand ocmdPCS =null;
			try
			{
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	
				ocmdPCS = null;

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLCASCADE_PREVENT_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.CASCADE_DELETE_PREVENT, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}
			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}

		public void Update(Object pobjObjecVO)
		{
			throw new NotImplementedException();
		}

		public void Add(Object pobjObjectVO)
		{
			throw new NotImplementedException();
		}

		public void UpdateDataSet(DataSet dstData)
		{
			const string METHOD_NAME = THIS + ".UpdateDataSet()";
			string strSql;
			OleDbConnection oconPCS =null;
			OleDbCommandBuilder odcbPCS ;
			OleDbDataAdapter odadPCS = new OleDbDataAdapter();
			try
			{
				strSql=	"SELECT "
					+ Pro_IssueSubMaterialDetailTable.ISSUESUBMATERIALDETAILID_FLD + ","
					+ Pro_IssueSubMaterialDetailTable.ISSUESUBMATERIALMASTERID_FLD + ","
					+ Pro_IssueSubMaterialDetailTable.PRODUCTID_FLD + ","
					+ Pro_IssueSubMaterialDetailTable.QUANTITY_FLD + ","
					+ Pro_IssueSubMaterialDetailTable.UMID_FLD 
					+ "  FROM " + Pro_IssueSubMaterialDetailTable.TABLE_NAME;

				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				odadPCS.SelectCommand = new OleDbCommand(strSql, oconPCS);
				odcbPCS = new OleDbCommandBuilder(odadPCS);
				dstData.EnforceConstraints = false;
				odadPCS.Update(dstData,Pro_IssueSubMaterialDetailTable.TABLE_NAME);

			}
			catch(OleDbException ex)
			{
				if(ex.Errors.Count >= 2)
				{
					if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
					{
																   
						throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
					}
					else if (ex.Errors[1].NativeError == ErrorCode.SQLCASCADE_PREVENT_KEYCODE)
					{

						throw new PCSDBException(ErrorCode.CASCADE_DELETE_PREVENT, METHOD_NAME, ex);				
					}
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}
			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}
			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}

		public DataSet GetDetailData(int intIssueSubMaterialMasterId)
		{
			const string METHOD_NAME = THIS + ".GetDetailData()";
			const string VIEW_PRO_ISSUESUBMATERIAL_DETAIL = "v_PRO_IssueSubMaterialDetail";
			DataSet dstPCS = new DataSet();
			
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				
				strSql=	"SELECT * "
					+ " FROM " + VIEW_PRO_ISSUESUBMATERIAL_DETAIL
					+ " WHERE " + Pro_IssueSubMaterialDetailTable.ISSUESUBMATERIALMASTERID_FLD + "=" + intIssueSubMaterialMasterId;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();
				
				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS,Pro_IssueSubMaterialDetailTable.TABLE_NAME);

				return dstPCS;
			}
			catch(OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}
	}
}
